package com.mcptt.config.body.location;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

public class LocationInfoBody_Request {

//	private static final String THIS_FILE = "LocationInfoBody_New";
	private static final String End_Mcptt_Tag = "</location-info>";
	
	private static final String Name_Request = "Request";
	private static final String Name_RequestId = "RequestId";
	
	private String requestId = null;
	private LocationInfoBody_Request()
	{}
	public LocationInfoBody_Request(String id)
	{
		this.requestId = id;
	}
	
	public static LocationInfoBody_Request parseXmlStr(String xmlStr) throws Exception 
	{    
		LocationInfoBody_Request body = new LocationInfoBody_Request();
		
	    int index = xmlStr.indexOf(End_Mcptt_Tag);
//	    Log.d(THIS_FILE, "McpttInfoBody_NewCall parseXmlStr index = " + index);
	    
//	    if(index <= 0)
//	    {
//	    	key = End_Mcvideo_Tag;
//	    	index = xmlStr.indexOf(key);
//	    }
	    xmlStr= xmlStr.substring(0, index + End_Mcptt_Tag.length());
	    xmlStr = xmlStr.trim();

		ByteArrayInputStream strIs = new  ByteArrayInputStream(xmlStr.getBytes());
		XmlPullParser pullParser = Xml.newPullParser();
		pullParser.setInput(strIs, "UTF-8"); //为Pull解释器设置要解析的XML数据        
		int event = pullParser.getEventType();

		while (event != XmlPullParser.END_DOCUMENT){

			switch (event) {

			case XmlPullParser.START_DOCUMENT:         
				break;    
			case XmlPullParser.START_TAG:    
				if (Name_Request.equals(pullParser.getName()))
				{
					body.requestId = pullParser.getAttributeValue("", Name_RequestId);
				} 
				break;

			case XmlPullParser.END_TAG:
				break;

			}

			event = pullParser.next();
		}

		return body;
	}
	
	public String toString()
	{
		StringBuffer strBuff = new StringBuffer();
//		strBuff.append("scop = ").append(scop).append(",");
		strBuff.append("id = ").append(requestId).append(".");
//		strBuff.append("timeInterval = ").append(timeInterval).append(",");
//		strBuff.append("distanceInterval = ").append(distanceInterval).append(".");
//		strBuff.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").append("\r\n");
//		strBuff.append("<location-info xmlns=\"urn:3gpp:ns:mcpttLocationInfo:1.0\">").append("\r\n");
//		strBuff.append("<Request RequestId=\"").append(mcpttId).append("\"/>").append("\r\n");
//
//		strBuff.append("</location-info>");
//
//		try {
//			return new String(strBuff.toString().getBytes(),"UTF-8");
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
		return strBuff.toString();
	}
	
	public String getRequestId() {
		return requestId;
	}
	public static String getTestStr(String mcpttId)
  {
	  StringBuffer strBuff = new StringBuffer();
    strBuff.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    strBuff.append("<location-info xmlns=\"urn:3gpp:ns:mcpttLocationInfo:1.0\">");
    strBuff.append("<Request RequestId=\"").append(mcpttId).append("\"/>");
    
    strBuff.append("</location-info>");
    try {
      return new String(strBuff.toString().getBytes(),"UTF-8");
    } catch (UnsupportedEncodingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    
    return strBuff.toString();
  }
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
